package com.sky.controller.admin;

import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.PageDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);
        dishService.cleanRedisCache(RedisConstant.DISH_BY_CATEGORY_ID + dishDTO.getCategoryId());
        return Result.success();
    }


    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageDTO<DishVO>> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageDTO<DishVO> pageDTO = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageDTO);
    }

    @ApiOperation("菜品批量删除")
    @DeleteMapping
    public Result deleteDish(@RequestParam("ids") List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);
        dishService.cleanRedisCache(RedisConstant.DISH_BY_CATEGORY_ID + "*");
        return Result.success();
    }


    @ApiOperation("修改菜品")
    @PutMapping
    public Result editDish(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        dishService.cleanRedisCache(RedisConstant.DISH_BY_CATEGORY_ID + dishDTO.getCategoryId());
        return Result.success();
    }


    @ApiOperation("根据id查询菜品和口味")
    @GetMapping("/{id}")
    public Result<DishVO> queryDishWithFlavor(@PathVariable Long id) {
        log.info("根据id查询菜品和口味：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }


    @ApiOperation("修改菜品状态")
    @PostMapping("/status/{status}")
    public Result editDish(@PathVariable Integer status, long id) {
        log.info("修改菜品为:{}, 商品id:{}", status == StatusConstant.ENABLE ? "启售" : "停售", id);
        dishService.StartOrStop(status, id);
        dishService.cleanRedisCache(RedisConstant.DISH_BY_CATEGORY_ID + "*");
        return Result.success();
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> queryDish(long categoryId) {
        log.info("根据分类id查询菜品:{}", categoryId);
        List<DishVO> list = dishService.queryDishByCategoryId(categoryId);
        return Result.success(list);
    }


}
